﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebStore.Web.App.Models.Account;
using WebStore.Web.App.Utility.Enum;

namespace WebStore.Web.App.Controllers
{
    public partial class AccountController
    {
        public ViewResult LogOn()
        {
            return View(new LogOnModel());
        }
        

        [HttpPost]
        [ActionName("LogOn")]
        public ActionResult LogOnPost(string returnUrl)
        {
            var model = new LogOnModel();
            if (!TryUpdateModel(model) || !ModelState.IsValid)
                return View(model);
            string errorMessage;
            if (!_accountService.LogOn(model.Email, model.Password, out errorMessage))
            {
                ModelState.AddModelError("", errorMessage);
                return View(model);
            }
            FormsAuthentication.SetAuthCookie(model.Email, true);
            return Redirect(returnUrl);
        }
    }
}